#coding:utf8
'''
Created on 2016年6月11日

@author: zhangjinrui
'''


import numpy as np
from matplotlib.backend_bases import key_press_handler

from matplotlib.figure import Figure
from matplotlib.lines import Line2D

class NodeFigure:
    
    def __init__(self):
        self.figure = Figure(dpi=100)
        
        self.cpu_axes = self.figure.add_subplot(4,1,1)
        self.memory_axes = self.figure.add_subplot(4,1,2)
        self.disk_axes = self.figure.add_subplot(4,1,3)
        self.net_axes = self.figure.add_subplot(4,1,4)
        
#         rect = 0.05,0.75,0.9,0.2
#         self.cpu_axes = self.figure.add_axes(rect)
        
#         self.cpu_axes.set_title("CPU")
#         self.cpu_axes.set_yticks((0.5,1.0))
        self.cpu_axes.plot([],[])
        self.cpu_axes.set_yticks((50,100))
        self.cpu_axes.set_xticks(range(10))
        self.cpu_axes.set_xticklabels(range(10),visible = False)
        
#         self.memory_axes.set_title('MEMORY')
        self.memory_axes.plot([],[])
        self.memory_axes.set_xticks(range(10))
        self.memory_axes.set_yticks((50,100))
        self.memory_axes.set_xticklabels(range(10), visible = False)
        
        
#         self.disk_axes.set_title('DISK')
#         self.disk_axes.set_xticklabels([],visible = False)
        self.disk_axes.plot([],[])
        self.disk_axes.plot([],[])
        self.disk_axes.set_xticks(range(10))
        self.disk_axes.set_xticklabels(range(10), visible = False)
        self.disk_axes.set_yticks(())
#         self.disk_axes.set_yticklabels(range(10), visible = False)
        
#         self.net_axes.set_title('Net')
        self.net_axes.plot([],[])
        self.net_axes.plot([],[])
        self.net_axes.set_xticks(range(10))
        self.net_axes.set_xticklabels(range(10), visible = False)
        self.net_axes.set_yticks(())
#         self.net_axes.set_yticks((1000,5000))
    
    def update_data(self, dat , canvas=None):
        
        #更新CPU的值
        line, = self.cpu_axes.get_lines()
        y = list(line.get_ydata())
        y.append(dat['cpu_all_percent'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        line.set_data(x , y)
        
        #更新内存的值
        line, = self.memory_axes.get_lines()
        y = list(line.get_ydata())
        y.append(dat['memory_percent'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        line.set_data(x, y)
        
        #更新磁盘的值
        lines = self.disk_axes.get_lines()
        #先更新读入速度
        y = list(lines[0].get_ydata())
        y.append(dat['disk_read_speed'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        lines[0].set_data(x , y)
        #更新写入速度
        y = list(lines[1].get_ydata())
        y.append(dat['disk_write_speed'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        lines[1].set_data(x , y)
        maxv = np.max(y)
        if maxv > 100:
            maxv += 10
        else:
            maxv = 100
        self.disk_axes.set_ylim(0,maxv)
        self.disk_axes.set_yticks((0,maxv / 2,maxv))
        
        #更新网络的值
        lines = self.net_axes.get_lines()
        #更新发送速度
        y = list(lines[0].get_ydata())
        y.append(dat['net_send_speed'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        lines[0].set_data(x , y)
        #更新接受速度
        y = list(lines[1].get_ydata())
        y.append(dat['net_rec_speed'])
        if len(y) > 10:
            del y[0]
        x = range(len(y))
        lines[1].set_data(x , y)
        maxv = np.max(y)
        if maxv > 10:
            maxv += 10
        else:
            maxv = 10
        self.net_axes.set_ylim(0,maxv)
        self.net_axes.set_yticks((0,maxv / 2,maxv))
        
        
        
        
        
        
        
        
        
        
        
        
        
        
